﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.Prism;
using Tanmia.Basis.ViewInterface;
using Tanmia.Basis.ViewModel;
using Tanmia.Warehouse.Core;
using Tanmia.Warehouse.Domain;
using Tanmia.Warehouse.Presentation.Properties;

namespace Tanmia.Warehouse.Presentation.Scenarios
{
    [ViewInfo("استعراض الأقسام", Constants.RegionMain, Views.SectionBrowser)]
    public class SectionBrowserViewModel : BrowserViewModel<Section>
    {
        public override IEnumerable<Section> SelectedEntities
        {
            get { return ((IList<Section>)Entities.SourceCollection).Where(x => x.IsSelected); }
        }

        public ICommand ShowEditorCommand { get; private set; }

        public SectionBrowserViewModel(IShellView shellView, IRegionManager regionManager, IEventAggregator eventAggregator, IMessageService messageService, SectionDomain sectionObj)
            : base(shellView, regionManager, eventAggregator, messageService, sectionObj)
        {
            EntityName = "Section";

            ShowEditorCommand = new DelegateCommand(OnShowEditor, OnCanDelete);
        }

        protected virtual void OnShowEditor()
        {
            RegionManager.NavigateToEditor(Views.SectionEditor, CurrentEntity);
        }

        protected override string DeleteConfirmationMessage
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (Section section in SelectedEntities)
                    sb.AppendFormat("{0}, {1}{2}", section.SectionID, section.Name, Environment.NewLine);

                return string.Format(Resources.DeleteConfirmation, sb, SelectedEntities.Count());
            }
        }

        protected override void CurrentChangedHandler(object sender, EventArgs e)
        {
            base.CurrentChangedHandler(sender, e);
            ((DelegateCommand)ShowEditorCommand).RaiseCanExecuteChanged();
        }
    }
}
